package com.future.controller;

import javax.validation.constraints.NotNull;

import com.future.common.annotation.ControllerMapping;
import com.future.common.base.BaseController;
import com.future.dao.NoticeDao;
import com.future.model.Notice;
import com.future.requestbean.NoticeAdd;
import com.future.requestbean.NoticeUpdate;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.tx.Tx;

@ControllerMapping("/notice")
public class NoticeController extends BaseController {
	@Inject
	private NoticeDao noticeDao;

	public void queryNotice() {
		renderResult("notices", this.noticeDao.findAll());
	}

	@Before(Tx.class)
	public void deleteNotice(@NotNull(message = "请选择要删除的通知") Long noticeId) {
		this.noticeDao.deleteById(noticeId);
		renderSuccess();
	}

	@Before(Tx.class)
	public void addNotice() {
		NoticeAdd noticeAdd = getJsonBody(NoticeAdd.class);
		Notice notice = beanToModel(noticeAdd, Notice.class);
		this.noticeDao.save(notice);
		renderSuccess();
	}

	@Before(Tx.class)
	public void noticeUpdate() {
		Notice notice = getJsonBodyToModel(NoticeUpdate.class, Notice.class);
		this.noticeDao.update(notice);
		renderSuccess();
	}
}
